package com.example.blueconn.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.example.blueconn.R;
import com.example.blueconn.databinding.FragmentBlSendParamBinding;
import com.example.blueconn.manager.FastBleManager;
import com.example.blueconn.utils.DataUtils;
import com.example.blueconn.utils.ThreadPoolUtils;

/**
 * Created by zhudongao
 * Desc
 * Date 4/5/21
 */

public class BLSendParamFragment extends Fragment {
    private FragmentBlSendParamBinding dataBinding;

    public BLSendParamFragment() {
        // Required empty public constructor
    }

    public static BLSendParamFragment newInstance() {
        return new BLSendParamFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_bl_send_param, container, false);
        initView();
        return dataBinding.getRoot();
    }

    private void initView() {
        dataBinding.lightParam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = dataBinding.lightNumber.getText().toString();
                String slop = dataBinding.lightSlop.getText().toString();
                String node = dataBinding.lightNode.getText().toString();
                String slopb = dataBinding.lightSlopb.getText().toString();
                String nodeb = dataBinding.lightNodeb.getText().toString();
                String cmd = "DDFFF" + number + DataUtils.toHexString(slop) +
                        "AC" + DataUtils.toHexString(node) + "AC" +
                        DataUtils.toHexString(slopb) + "AC" +
                        DataUtils.toHexString(nodeb) + "CCCFCCCC";
                FastBleManager.getInstance().writeData(DataUtils.hexToBytes(cmd));
            }
        });

        dataBinding.allLightSend.setOnClickListener(v -> {
            String slop = dataBinding.lightSlop.getText().toString();
            String node = dataBinding.lightNode.getText().toString();
            String slopb = dataBinding.lightSlopb.getText().toString();
            String nodeb = dataBinding.lightNodeb.getText().toString();


            ThreadPoolUtils.submit(() -> {
                for (int i = 0; i < 6; i++) {
                    String cmd = "DDFFF" + (i + 1) + DataUtils.toHexString(slop) +
                            "AC" + DataUtils.toHexString(node) + "AC" +
                            DataUtils.toHexString(slopb) + "AC" +
                            DataUtils.toHexString(nodeb) + "CCCFCCCC";
                    FastBleManager.getInstance().writeData(DataUtils.hexToBytes(cmd));
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {

                    }
                }
            });
        });
        dataBinding.tempParam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = dataBinding.tempNumber.getText().toString();
                String slop = dataBinding.tempSlop.getText().toString();
                String node = dataBinding.tempNode.getText().toString();
                String cmd = "DDEEF" + number + DataUtils.toHexString(slop) +
                        "AC" + DataUtils.toHexString(node) + "CCCFCCCC";
                FastBleManager.getInstance().writeData(DataUtils.hexToBytes(cmd));
            }
        });
        dataBinding.allTempSend.setOnClickListener(v -> {
            String slop = dataBinding.tempSlop.getText().toString();
            String node = dataBinding.tempNode.getText().toString();
            ThreadPoolUtils.submit(() -> {
                for (int i = 0; i < 5; i++) {
                    String cmd = "DDEEF" + (i + 1) + DataUtils.toHexString(slop) +
                            "AC" + DataUtils.toHexString(node) + "CCCFCCCC";
                    FastBleManager.getInstance().writeData(DataUtils.hexToBytes(cmd));
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {

                }
            });
        });
    }
}
